package com.example.myview;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.myview.placeholder.ContactModel;
import com.example.myview.placeholder.CustomAdapter;
import com.example.myview.placeholder.PlaceholderContent;

import java.util.ArrayList;
import java.util.List;

/**
 * A fragment representing a list of Items.
 */
public class ItemFragment extends Fragment {

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ItemFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static ItemFragment newInstance(int columnCount) {
        ItemFragment fragment = new ItemFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);

        }
    }



        @Override
        public View onCreateView (LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState){
            View view = inflater.inflate(R.layout.fragment_item_list, container, false);

            // Set the adapter
            if (view instanceof RecyclerView) {
                Context context = view.getContext();
                RecyclerView recyclerView = (RecyclerView) view;
                if (mColumnCount <= 1) {
                    recyclerView.setLayoutManager(new LinearLayoutManager(context));
                } else {
                    recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
                }
                recyclerView.setAdapter(new MyItemRecyclerViewAdapter(PlaceholderContent.ITEMS));
            }
            return view;
        }
    private void initview() {
        //1.获取listview
        RecyclerView listview = findViewById(R.id.list_view);
        TextView addlist = findViewById(R.id.list_add);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        listview.setLayoutManager(llm);


        //2.绘制条目列表
        //3.创建CustomAdapter,重写里面的方法
        //4.创建ContactModel类，将每个控件的名字设为全局变量，并生成其set，get方法
        //5.在CustomAdapter类中创建private List<ContactModel>data;并生成其set方法跟构造方法；
        List<ContactModel> data = new ArrayList<>();
        ContactModel item = new ContactModel();
        item.setListicon(R.drawable.change);
        item.setListmax("5");
        item.setListtitle("嫦娥仙子");
        item.setListdate("12:20");
        item.setListtitle2("在干嘛");
        data.add(item);
        ContactModel houge = new ContactModel();
        houge.setListicon(R.drawable.houge);
        houge.setListmax("5");
        houge.setListtitle("猴哥");
        houge.setListdate("12:20");
        houge.setListtitle2("有个妖精搞不定，搭把手");
        data.add(houge);
        ContactModel sanzang = new ContactModel();
        sanzang.setListicon(R.drawable.sanzang);
        sanzang.setListmax("3");
        sanzang.setListtitle("唐僧");
        sanzang.setListdate("12:20");
        sanzang.setListtitle2("救救我，大哥");
        data.add(sanzang);
        ContactModel bajie = new ContactModel();
        bajie.setListicon(R.drawable.bajie);
        bajie.setListmax("6");
        bajie.setListtitle("八戒");
        bajie.setListdate("12:20");
        bajie.setListtitle2("八戒，你个懒虫");
        data.add(bajie);
        ContactModel gril = new ContactModel();
        gril.setListicon(R.drawable.grilwang);
        gril.setListmax("3");
        gril.setListtitle("女儿国主");
        gril.setListdate("12:20");
        gril.setListtitle2("哥哥，哥哥");
        data.add(gril);
        ContactModel guanyin = new ContactModel();
        guanyin.setListicon(R.drawable.guanyin);
        guanyin.setListmax("7");
        guanyin.setListtitle("观音");
        guanyin.setListdate("12:20");
        guanyin.setListtitle2("施主近来可好");
        data.add(guanyin);
        ContactModel jinjiao = new ContactModel();
        jinjiao.setListicon(R.drawable.jinjiao);
        jinjiao.setListmax("1");
        jinjiao.setListtitle("金角");
        jinjiao.setListdate("12:20");
        jinjiao.setListtitle2("哥，猴子打我");
        data.add(jinjiao);
        ContactModel rulai = new ContactModel();
        rulai.setListicon(R.drawable.rulai);
        rulai.setListmax("1");
        rulai.setListtitle("如来");
        rulai.setListdate("12:20");
        rulai.setListtitle2("兄弟，还好吗");
        data.add(rulai);
        ContactModel shasen = new ContactModel();
        shasen.setListicon(R.drawable.shaseng);
        shasen.setListmax("1");
        shasen.setListtitle("沙和尚");
        shasen.setListdate("12:20");
        shasen.setListtitle2("师傅被妖怪抓走了");
        data.add(shasen);
        ContactModel yinjiao = new ContactModel();
        yinjiao.setListicon(R.drawable.yinjiao);
        yinjiao.setListmax("1");
        yinjiao.setListtitle("银角");
        yinjiao.setListdate("12:20");
        yinjiao.setListtitle2("今晚去蹦迪否");
        data.add(yinjiao);
        ContactModel yudi = new ContactModel();
        yudi.setListicon(R.drawable.yudi);
        yudi.setListmax("1");
        yudi.setListtitle("玉帝");
        yudi.setListdate("12:20");
        yudi.setListtitle2("七仙女还可以吗，哥");
        data.add(yudi);
        ContactModel yaojing = new ContactModel();
        yaojing.setListicon(R.drawable.zhizhujing);
        yaojing.setListmax("1");
        yaojing.setListtitle("蜘蛛精");
        yaojing.setListdate("12:20");
        yaojing.setListtitle2("奴家仰慕官人已久");
        data.add(yaojing);
        CustomAdapter adapter = new CustomAdapter(this);
        adapter.setData(data);
        listview.setAdapter(adapter);
    }}
    }